<?php
 class Db {
 
	const SUCCESS = 800;
	const FAIL = 0;
	const DATA_EXISTS = 1;
	
	function __construct () {
		if(file_exists('../inc_/s3ccfrt.php')) {
			include('../inc_/s3ccfrt.php');
		} else {
			include('inc_/s3ccfrt.php');
		}
		$this->mysqli = new mysqli($host, $user, $password, $database);
	}
	private function test_connection() {
		if($this->mysqli->connect_errno) {
			die(mysqli_connect_error());
			exit();
		}
	}
	public function select($sql) {
		$this->test_connection();
		if($this->mysqli->query($sql)) {
			$result = $this->mysqli->query($sql);
			if($result->num_rows <= 0) {
				return self::FAIL;
			} else {
				while($row = $result->fetch_array(MYSQLI_ASSOC)) {
					$array[] = $row;
				}
				//$this->free_all($result);
				return $array;
			}
		}
	}
	
	public function insert($sql) {
		$this->test_connection();
		if($this->mysqli->query($sql)) {
			return $this->mysqli->insert_id;
		}
		return self::FAIL;
	}
	
	public function delete($sql) {
		$this->test_connection();
		if($this->mysqli->query($sql)) {
			return self::SUCCESS;
		} else {
			return self::FAIL;
		}
	}
	
	public function update($sql) {
		$this->test_connection();
		if($this->mysqli->query($sql)) {
			return self::SUCCESS;
		} else {
			return self::FAIL;
		}
	}
	
	public function count_rows($rows) {
		return count($rows);
	}
 }
?>